qui {
	capture program drop ccn_newsym_flexdelta
	*************************************************************************************
	*************************************************************************************
	*************************************************************************************
	*************************************************************************************
	*************************************************************************************
	local count=0
	local subsample = "$subsample"
	foreach lhs in $LHSlist { 
		local controls="${CONTROLlist_`lhs'}"
		foreach rhs in $RHSlist { 
			foreach robust in $robust_list {
				forvalues K=$K_list {
					noisily display "LHS=`lhs', RHS=`rhs', SE=`robust', K=`K', $S_DATE, $S_TIME"
					qui {
						local count=`count'+1
						matrix actual_estimates_mat = J($boot_reps,18,-9)
						use "$datapath/003_cluster_`subsample'_`lhs'.dta", clear
						local control_length = 0
						foreach control of varlist `controls' {
							local control_length = `control_length' + 1
						}
						global control_length = `control_length'
						qui tab X_`K', generate(dumX_`K'_)
						tempfile temp
						save `temp', replace
						local rowcounter = 1
						forvalues i=1(1)$boot_reps {
							noisily display "Iteration `i', $S_DATE, $S_TIME"
							use "`temp'", clear
							bsample 

							qui ccn_newsym_flexdelta naive `K' `rhs' `lhs' `robust' "`controls'" het_tobit
							matrix actual_estimates_mat[`rowcounter',1] = r(BETA)
							matrix actual_estimates_mat[`rowcounter',2] = r(DELTA_top)
							matrix actual_estimates_mat[`rowcounter',3] = e(N)

							qui ccn_newsym_flexdelta tobit `K' `rhs' `lhs' `robust' "`controls'" het_tobit
							matrix actual_estimates_mat[`rowcounter',4] = r(BETA)
							matrix actual_estimates_mat[`rowcounter',5] = r(DELTA)
							matrix actual_estimates_mat[`rowcounter',6] = r(DELTA_top)

							qui ccn_newsym_flexdelta het_tobit `K' `rhs' `lhs' `robust' "`controls'" het_tobit
							matrix actual_estimates_mat[`rowcounter',7] = r(BETA)
							matrix actual_estimates_mat[`rowcounter',8] = r(DELTA)
							matrix actual_estimates_mat[`rowcounter',9] = r(DELTA_top)

							qui ccn_newsym_flexdelta symmetric `K' `rhs' `lhs' `robust' "`controls'" het_tobit
							matrix actual_estimates_mat[`rowcounter',10] = r(BETA)
							matrix actual_estimates_mat[`rowcounter',11] = r(DELTA)
							matrix actual_estimates_mat[`rowcounter',12] = r(DELTA_top)
							
							qui ccn_newsym_flexdelta het_uniform `K' `rhs' `lhs' `robust' "`controls'" het_tobit
							matrix actual_estimates_mat[`rowcounter',13] = r(BETA)
							matrix actual_estimates_mat[`rowcounter',14] = r(DELTA)
							matrix actual_estimates_mat[`rowcounter',15] = r(DELTA_top)
							
							qui ccn_newsym_flexdelta naiveN `K' `rhs' `lhs' `robust' "`controls'" het_tobit
							matrix actual_estimates_mat[`rowcounter',16] = r(BETA)

							matrix actual_estimates_mat[`rowcounter',17] = `K'
							matrix actual_estimates_mat[`rowcounter',18] = `i'

							local rowcounter = `rowcounter'+1
						}
						
						mat colnames actual_estimates_mat = b_Naive dt_Naive N b_Tobit d_Tobit dt_Tobit b_HetTobit d_HetTobit dt_HetTobit ///
							b_HetSymm d_HetSymm dt_HetSymm b_HetUni d_HetUni dt_HetUni  b_NaiveN K iteration
						
						clear
						qui svmat actual_estimates_mat, names(col)
						
						collapse b_Naive dt_Naive N b_Tobit d_Tobit dt_Tobit b_HetTobit d_HetTobit dt_HetTobit b_HetSymm d_HetSymm dt_HetSymm b_HetUni d_HetUni dt_HetUni b_NaiveN K ///
							(sd) sd_Naive=b_Naive sd_dt_Naive=dt_Naive sd_Tobit=b_Tobit sd_d_Tobit=d_Tobit sd_dt_Tobit=dt_Tobit sd_HetTobit=b_HetTobit ///
							sd_d_HetTobit=d_HetTobit sd_dt_HetTobit=dt_HetTobit sd_HetSymm=b_HetSymm sd_d_HetSymm=d_HetSymm sd_dt_HetSymm=dt_HetSymm sd_HetUni=b_HetUni sd_d_HetUni=d_HetUni sd_dt_HetUni=dt_HetUni sd_NaiveN=b_NaiveN ///
							(count) Niter=b_Naive
						
						gen se_Naive=sd_Naive
						gen se_dt_Naive = sd_dt_Naive
						gen se_NaiveN=sd_NaiveN
						foreach method in Tobit HetTobit HetSymm HetUni {
							gen se_`method'=sd_`method'
							gen se_d_`method'=sd_d_`method'
							gen se_dt_`method'=sd_dt_`method'
						}
						
						keep b_* d_* dt_* se_* Niter N
					
						gen LHS="`lhs'"
						gen RHS="`rhs'"
						gen Kcluster=`K'
						gen Robust="`robust'"
						save "$datapath/004_hom_beta_flex_delta_results_`subsample'_`lhs'_`robust'_K`K'BOOT.dta", replace
					}
				}		
			}
		}
	}
}


